# Maintainer: Jan Alexander Steffens (heftig) <heftig@archlinux.org>

pkgbase=pahole
pkgname=(
  pahole
  ostra-cg
)
pkgver=1.25
pkgrel=4
epoch=1
pkgdesc="Pahole and other DWARF utils"
url="https://git.kernel.org/pub/scm/devel/pahole/pahole.git"
arch=(x86_64)
license=(GPL2)
makedepends=(
  bash
  cmake
  libelf
  ninja
  python
  python-matplotlib
  zlib
)
_commit=02d67c51765dfbd5893087da63744c864c7cc9e0  # master
source=(
  https://fedorapeople.org/~acme/dwarves/dwarves-$pkgver.tar.{xz,sign}
  python.diff
)
b2sums=('1926fa1fa123fc3ad0f7f063406260b1e1e2611c563fedebee4c837e491164571fdb40408421c0c4ea2fd24e89c54a7a1ea669313b6dd6d7dcfa4934e2c1336e'
        'SKIP'
        'e88fcda77c0a6aaea0d83949cabbaaaf24f6a4b2324a14e459efc202b210c31b58f7a90c75c34ffd08911514b3b6db4be4423e3f8e0fb50b6d914da5be002319')
validpgpkeys=(
  2DBF5BAA46FB4DED338A335BD65016F35352AA40  # Arnaldo Carvalho de Melo <acme@kernel.org>
)

prepare() {
  cd dwarves-$pkgver

  # https://bugs.archlinux.org/task/70013
  patch -Np1 -i ../python.diff
}

build() {
  local cmake_options=(
    -DCMAKE_INSTALL_PREFIX=/usr
    -DCMAKE_BUILD_TYPE=None
    -D__LIB=lib
  )

  cmake -S dwarves-$pkgver -B build -G Ninja "${cmake_options[@]}"
  cmake --build build
}

check() {
  cd build
  ctest --output-on-failure --stop-on-failure -j$(nproc)
}

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#$pkgdir/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}

package_pahole() {
  depends=(
    bash
    libelf
    zlib
  )
  optdepends=('ostra-cg: Generate call graphs from encoded traces')
  provides=(libdwarves{,_emit,_reorganize}.so)

  DESTDIR="$pkgdir" cmake --install build

  _pick ostra "$pkgdir"/usr/{bin/ostra-cg,lib/python*}
}

package_ostra-cg() {
  pkgdesc="Generate call graphs from encoded traces"
  depends=(
    pahole
    python
    python-matplotlib
  )

  mv ostra/* "$pkgdir"

  python -m compileall -d /usr/lib "$pkgdir/usr/lib"
  python -O -m compileall -d /usr/lib "$pkgdir/usr/lib"
}

# vim:set sw=2 sts=-1 et:
